Ext.define('SKJF.controller.PURCHASERTNDET',{
	extend: 'Ext.app.Controller',
	views: ['PEMBELIAN.v_purchasertndet','PEMBELIAN.v_purchasertndet_form'],
	models: ['m_purchasertndet'],
	stores: ['s_purchasertndet'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listpurchasertn',
		selector: 'Listpurchasertn'
	}, {
		ref: 'Listpurchasertndet',
		selector: 'Listpurchasertndet'
	}, {
		ref: 'v_purchasertndet_form',
		selector: 'v_purchasertndet_form'
	}, {
		ref: 'Savepurchasertndet',
		selector: 'v_purchasertndet_form #save'
	}, {
		ref: 'Createpurchasertndet',
		selector: 'v_purchasertndet_form #create'
	}, {
		ref: 'PURCHASERTNDET',
		selector: 'PURCHASERTN #south'
	}],


	init: function(){
		this.control({
			'PURCHASERTNDET': {
				'afterrender': this.purchasertndetAfterRender
			},
			'v_purchasertndet_form': {
				'afterlayout': this.purchasertndetAfterLayout
			},
			'Listpurchasertndet': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListpurchasertndet
			},
			'Listpurchasertndet button[action=create]': {
				click: this.createRecord
			},
			'Listpurchasertndet button[action=delete]': {
				click: this.deleteRecord
			},
			'Listpurchasertndet button[action=print]': {
				click: this.printRecords
			},
			'v_purchasertndet_form button[action=save]': {
				click: this.saveV_purchasertndet_form
			},
			'v_purchasertndet_form button[action=create]': {
				click: this.saveV_purchasertndet_form
			},
			'v_purchasertndet_form button[action=cancel]': {
				click: this.cancelV_purchasertndet_form
			}
		});
	},
	
	purchasertndetAfterRender: function(){
		//var purchasertndetStore = this.getListpurchasertndet().getStore();
		//purchasertndetStore.load();
	},
	
	purchasertndetAfterLayout: function(){
		this.getV_purchasertndet_form().down('#TGL_DETAIL_PURCHASE_RETURN_field').focus(false, true);
		// this.getV_purchasertndet_form().down('#NOMOR_LPB_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListpurchasertndet	= this.getListpurchasertndet();
		var getV_purchasertndet_form= this.getV_purchasertndet_form(),
			form			= getV_purchasertndet_form.getForm();
		var getSavepurchasertndet	= this.getSavepurchasertndet();
		var getCreatepurchasertndet	= this.getCreatepurchasertndet();
		var sel_purchasertn = this.getListpurchasertn().getSelectionModel().getSelection()[0];
		
		/* grid-panel */
		getListpurchasertndet.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_purchasertndet_form.down('#NOMOR_PURCHASE_RETURN_field').setReadOnly(true);
		getV_purchasertndet_form.down('#NOMOR_LPB_field').setReadOnly(true);
		
		if(sel_purchasertn != null){
			getV_purchasertndet_form.down('#NOMOR_PURCHASE_RETURN_field').setValue(sel_purchasertn.data.NOMOR_PURCHASE_RETURN);
			getV_purchasertndet_form.down('#NOMOR_LPB_field').setValue(sel_purchasertn.data.NOMOR_LPB);
		}
		
		getSavepurchasertndet.setDisabled(true);
		getCreatepurchasertndet.setDisabled(false);
		getV_purchasertndet_form.setDisabled(false);
		
		this.getPURCHASERTNDET().setActiveTab(getV_purchasertndet_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListpurchasertndet().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListpurchasertndet: function(me, record, item, index, e){
		var getPURCHASERTNDET		= this.getPURCHASERTNDET();
		var getListpurchasertndet	= this.getListpurchasertndet();
		var getV_purchasertndet_form= this.getV_purchasertndet_form(),
			form			= getV_purchasertndet_form.getForm();
		var getSavepurchasertndet	= this.getSavepurchasertndet();
		var getCreatepurchasertndet	= this.getCreatepurchasertndet();
		
		getSavepurchasertndet.setDisabled(false);
		getCreatepurchasertndet.setDisabled(true);
		getV_purchasertndet_form.down('#NOMOR_PURCHASE_RETURN_field').setReadOnly(true);getV_purchasertndet_form.down('#NOMOR_LPB_field').setReadOnly(true);		
		getV_purchasertndet_form.loadRecord(record);
		
		getListpurchasertndet.setDisabled(true);
		getV_purchasertndet_form.setDisabled(false);
		getPURCHASERTNDET.setActiveTab(getV_purchasertndet_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListpurchasertndet().getStore();
		var selection = this.getListpurchasertndet().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_PURCHASE_RETURN" = "'+selection.data.NOMOR_PURCHASE_RETURN+'","NOMOR_LPB" = "'+selection.data.NOMOR_LPB+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListpurchasertndet().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_purchasertndet/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/purchasertndet.html','purchasertndet_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_purchasertndet_form: function(){
		var getPURCHASERTNDET		= this.getPURCHASERTNDET();
		var getListpurchasertndet 	= this.getListpurchasertndet();
		var getV_purchasertndet_form= this.getV_purchasertndet_form(),
			form			= getV_purchasertndet_form.getForm(),
			values			= getV_purchasertndet_form.getValues();
		var store 			= this.getStore('s_purchasertndet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_purchasertndet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_PURCHASE_RETURN') === values.NOMOR_PURCHASE_RETURN && record.get('NOMOR_LPB') === values.NOMOR_LPB) {
										return true;
									}
									return false;
								}
							);
							/* getListpurchasertndet.getView().select(recordIndex); */
							getListpurchasertndet.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_purchasertndet_form.setDisabled(true);
					getListpurchasertndet.setDisabled(false);
					getPURCHASERTNDET.setActiveTab(getListpurchasertndet);
				}
			});
		}
	},
	
	createV_purchasertndet_form: function(){
		var getPURCHASERTNDET		= this.getPURCHASERTNDET();
		var getListpurchasertndet 	= this.getListpurchasertndet();
		var getV_purchasertndet_form= this.getV_purchasertndet_form(),
			form			= getV_purchasertndet_form.getForm(),
			values			= getV_purchasertndet_form.getValues();
		var store 			= this.getStore('s_purchasertndet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_purchasertndet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_purchasertndet_form.setDisabled(true);
					getListpurchasertndet.setDisabled(false);
					getPURCHASERTNDET.setActiveTab(getListpurchasertndet);
				}
			});
		}
	},
	
	cancelV_purchasertndet_form: function(){
		var getPURCHASERTNDET		= this.getPURCHASERTNDET();
		var getListpurchasertndet	= this.getListpurchasertndet();
		var getV_purchasertndet_form= this.getV_purchasertndet_form(),
			form			= getV_purchasertndet_form.getForm();
			
		form.reset();
		getV_purchasertndet_form.setDisabled(true);
		getListpurchasertndet.setDisabled(false);
		getPURCHASERTNDET.setActiveTab(getListpurchasertndet);
	}
	
});